#include "server.hpp"

#define HOST "127.0.0.1"
#define PORT 3306
#define USER "root"
#define PASSWORD "0000"
#define DBNAME "gobang"

void mysql_test() 
{
    MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASSWORD, DBNAME, PORT);
    const char *sql = "insert stu values(null, 'jack', 18, 55.2, 68.5, 87);";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if (ret == false) 
    {
        return ;
    }
    mysql_util::mysql_destroy(mysql);
}

void json_test() {
    Json::Value root;
    std::string body;
    root["name"] = "jim";
    root["age"] = 18;
    root["score"].append(96);
    root["score"].append(78.5);
    root["score"].append(82);
    json_util::serialize(root, body);
    DLOG("%s", body.c_str());
    
    Json::Value val;
    json_util::unserialize(body, val);
    std::cout << "name:" << val["name"].asString()  << std::endl;
    std::cout << "age:" << val["age"].asInt()  << std::endl;
    int sz = val["score"].size();
    for (int i = 0; i < sz; i++) 
    {
        std::cout << "score: " << val["score"][i].asFloat() << std::endl;
    }
}

void str_test() 
{
    std::string str = ",...,,123,234,,,,,345,,,,";
    std::vector<std::string> arry;
    string_util::split(str, ",", arry);
    for (auto s : arry) {
        DLOG("%s", s.c_str());
    }
}

void filetest()
{
    std::string filename = "./makefile";
    std::string body;
    file_util::read(filename,body);
    std::cout << body << std::endl;
}

void db_test()
{
    user_table ut(HOST, USER, PASSWORD, DBNAME, PORT);
    Json::Value user;
    user["username"] = "sally";
    //user["password"] = "11111111";
    bool ret = ut.insert(user);
    
}

void online_test()
{
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid = 1111;
    om.enter_game_hall(uid,conn);
    if(om.is_in_game_hall(1111))
    {
        DLOG("in the hall!");
    }

    om.exit_game_hall(uid);
    if(!om.is_in_game_hall(1111))
    {
        DLOG("not in the hall!");
    }
}

void room_test()
{   
    user_table ut(HOST, USER, PASSWORD, DBNAME, PORT);
    online_manager om;
    room_manager rm(&ut,&om);
    rm.create_room(10,20);
}

void macher_test()
{
    user_table ut(HOST, USER, PASSWORD, DBNAME, PORT);
    online_manager om;
    room_manager rm(&ut,&om);
    rm.create_room(10,20);
    matcher mc(&rm,&ut,&om);
}


int main()
{
    //mysql_test();
    //json_test();
    //str_test();
    //filetest();
    //db_test();
    //online_test();
    //room_test();
    //macher_test();

    gobang_server _server(HOST,USER, PASSWORD, DBNAME, PORT);
    _server.start(8080);
    

    return 0;
}